<?php
/**
 * Created by PhpStorm.
 * User: tony
 * Date: 2018/6/4
 * Time: 11:20
 */

namespace app\modules\v1\controllers;

use app\config\Status;

use app\models\Project;
use app\models\ProjectYt;
use app\models\User;
use app\modules\v1\controllers\parent\ParentController;
use yii\data\ActiveDataProvider;
use Yii;

class ProjectYtController extends ParentController
{
    // 获取我的任务
    public function actionTask() {
        $params = Yii::$app->request->getQueryParams();
        $id   = Yii::$app->user->id; // 根据token获取到的id
        $user = User::findOne($id);
        $query = ProjectYt::find()->where(['project_manage' => $user['username']])->asArray()->joinWith('project');

        if (isset($params['branch_id'])) {
            $query->andWhere(['hj_project.branch_id' => $params['branch_id']]);
        }

        if (isset($params['name'])) {
            $query->andWhere(['like', 'hj_project.name', $params['name']]);
        }

        if (isset($params['scale'])) {
            $query->andWhere(['scale'=>  $params['scale']]);
        }

        if (isset($params['project_type_id'])) {
            $query->andWhere(['hj_project.project_type_id'=> $params['project_type_id']]);
        }

        if (isset($params['customer_id'])) {
            $query->andWhere(['hj_project.customer_id'=> $params['customer_id']]);
        }

        if (isset($params['start_at'])) {
            $query->andWhere(['>=', 'hj_project.plan_started_at', $params['start_at'] / 1000]);
        }

        if (isset($params['end_at'])) {
            $query->andWhere(['<=', 'hj_project.plan_started_at', $params['end_at'] / 1000]);
        }

        if (isset($params['project_id'])) {
            $query->andWhere(['project_id'=>  $params['project_id']]);
        }
        $pageSize = 5;
        if(isset($params['pageSize'])){
            $pageSize = (int)$params['pageSize'];
        }
        return new ActiveDataProvider([
            // 使用with方法实现贪加载
            'query' => $query,
            // 分页信息
            'pagination' => [
                'pageSize' => $pageSize,
            ],
            // 排序信息
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);
    }

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionUpdate() {
        $data = Yii::$app->request->getBodyParams();
        $model = ProjectYt::findOne(['id' => (int)$data['id']]);
        $model->load($data, '');
        if ($model->save()) {
            return [
                'bCode' => Status::SUCCESS,
                'bData' => $model,
            ];
        }
        return [
            'bCode' => Status::FAIL,
            'bData' => $model->errors,
        ];
    }

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionView() {
        $params = Yii::$app->request->getQueryParams();
        $result = ProjectYt::findOne(['project_id' => (int)$params['project_id']]);
        return $result;
    }
}




